<template>
    <div class="visual-preview">
        <header>
            <h3>预览区域 <a href="javascript:;" @click="switchState()">{{state ? '编辑' : '预览'}}</a></h3>
            <ul>
                <li
                    v-for="(item, index) in handleList"
                    :key="index"
                    @click="handle(item.fn)">
                    <i class="iconfont" :class="`icon-${item.icon}`"></i>
                    <span>{{ item.title }}</span>
                </li>
            </ul>
        </header>
        <section>
            <div class="phone-edit">
               <draggable
                    v-model="modules"
                    :options="draggableOptions"
                    @start="isDragging=true"
                    @end="isDragging=false"
                    @add="addDragg($event)"
                    style="min-height:10rem; height:auto;">
                    <div
                        v-for="(item, index) in modules"
                        class="module-component"
                        :class="{active: item.setting}"
                        :key="index">
                        <div
                            class="module-component__active"
                            @click="moduleSetting({ index })"
                            v-show="!state">
                           <footer>
                                <span @click.stop="removeModule({ index })">
                                    删除
                                </span>
                                <span class="handle">
                                    拖动
                                </span>
                           </footer>
                        </div>
                        <component
                            :is="`ec-${item.module}`"
                            :data="item.data"
                            :preview="true">
                        </component>
                        
                    </div>
               </draggable>
            </div>
        </section>
    </div>
</template>
<script>
// mapActions
import { mapActions } from 'vuex'

// draggable
import Draggable from 'vuedraggable'

// viusal components
import { EcBlank, EcTitle, EcNav } from '@/components'

export default {
    components: {
        Draggable,
        EcBlank,
        EcTitle,
        EcNav
    },
    data() {
        return {
            /**
             * 切换预览编辑状态
             * 0 等于 编辑状态 / 1 等于 预览状态
             *
             * @type {Number}
             */
            state: 0,
            /**
             * 拖拽组件配置
             *
             * @type {Object}
             */
            draggableOptions: {
                group: 'fromList',
                handle: '.handle'
            },
            /**
             * 操作按钮
             *
             * @type {Array.<Object>}
             */
            handleList: [
                {
                    /**
                     * icon 图表
                     */
                    icon: 'clear',
                    /**
                     * 显示文本
                     */
                    title: '清空',
                    /**
                     * 触发的方法名称
                     */
                    fn: 'clear'
                },
                {
                    icon: 'restore',
                    title: '还原',
                    fn: 'restore'
                },
                {
                    icon: 'save',
                    title: '存储',
                    fn: 'save'
                }
            ]
        }
    },
    computed: {
        modules: {
            get() {
                return this.$store.state.viusal.modules
            },
            set(value) {
                this.updateModules({ modules: value })
            }
        },
        list: {
            get() {
                return this.data.list
            },
            set(value) {
                this.updateList(value)
            }
        }
    },
    methods: {
        ...mapActions('viusal', [
            'updateModules',
            'addModule',
            'removeModule',
            'updateDataAttr',
            'moduleSetting',
            'updateList'
        ]),
        /**
         * 中转方法，触发相应的操作
         *
         * @param {String} fn - 方法名称
         */
        handle(fn) {
            this[fn]()
        },
        clear() {
            console.log('clear')
        },
        restore() {
            console.log('restore')
        },
        save() {
            console.log('save')
        },
        /**
         * 修改状态
         */
        switchState() {
            this.state = this.state === 0 ? 1 : 0
        },
        /**
         * 从组件库拖动过来后新增组件
         *
         * @param {Object} e - 当前事件
         */
        addDragg(e) {
            e.item.outerHTML = '' // 拖拽添加后删除该内容'
            this.addModule({
                /**
                 * 添加后的索引位置
                 *
                 * @param {Number} newIndex
                 */
                newIndex: e.newIndex,
                /**
                 * module 初始化信息
                 *
                 * @param {Object} module
                 */
                module: this.cloneModule(e.item.getAttribute('data-module'))
            })
        },
        /**
         * 克隆module
         *
         * @param {String} moduleName - 模块名称 引入的路径
         */
        cloneModule(moduleName) {
            delete require.cache[
                require.resolve(`@/components/${moduleName}/data/online`)
            ] // 删除 require 引用缓存
            return require(`@/components/${moduleName}/data/online`)
        }
    }
}
</script>
<style lang="less" scoped>
@import '~@/assets/style/theme/index';
.visual-preview {
    position: absolute;
    left: 231px;
    top: 1px;
    bottom: 0;
    width: 454px;
    border-right: 1px solid @border-color-split;
    header {
        padding: 12px @gap;
        height: 46px;
        background: @table-thead-bg;
        display: flex;
        align-items: center;
        justify-content: space-between;
        h3 {
            font-size: 14px;
        }
        a {
            font-weight: normal;
            font-size: 12px;
            margin-left: 6px;
        }
        ul {
            li {
                display: inline-block;
                margin-left: 10px;
                cursor: pointer;
                i {
                    margin-right: 1px;
                }
                span {
                    font-size: 13px;
                }
            }
        }
    }
    section {
        display: block;
        width: 336px;
        height: 661px;
        background: url('~@/assets/images/iphone_x.png');
        background-size: cover;
        margin: 0 auto;
        margin-top: 36px;
        position: relative;
        .phone-edit {
            position: absolute;
            top: 73px;
            left: 13px;
            right: 12px;
            bottom: 30px;
            // border-radius: 0 0 20px 20px;
            overflow-y: auto;
            &::-webkit-scrollbar {
                display: none;
            }
        }
    }
}
.module-component {
    position: relative;
    min-height: 20px;
    &:hover,
    &.active {
        .module-component__active {
            border: 1px dashed rgba(39, 185, 215, 1);
        }
        .module-component__active {
            display: inherit;
        }
    }
}
.module-component__active {
    user-select: none;
    position: absolute;
    right: 0;
    top: 0;
    left: 0;
    bottom: 0;
    position: absolute;
    display: none;
    z-index: 11;
    span {
        display: block;
        float: left;
        padding: 0 6px;
        cursor: pointer;
        background-color: rgba(39, 185, 215, 0.8);
        color: #fff;
    }
    span:last-child {
        margin-left: 1px;
        cursor: move;
    }
    footer {
        position: absolute;
        right: 0;
        bottom: 0;
    }
}
.phone-edit{
    background-color: @table-thead-bg;
}
</style>